package cn.web.tunnelrobot.controller;

import cn.web.tunnelrobot.dto.UserDTO;
import cn.web.tunnelrobot.pojo.User;
import cn.web.tunnelrobot.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * 用户
 * @version 1.0.0
 *
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户注册，登录等相关接口")
@ApiSort(value = 1)
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param usAccount
     * @param usPassword
     * @param usRole
     * @param session
     * @return
     * @throws Exception
     */
    @GetMapping("/login")
    @ResponseBody
    @ApiOperation(value = "账户，密码，用户角色", notes = "登陆")
    public HashMap<String, Object> login(@RequestParam(required = false) String usAccount,
                                         @RequestParam(required = false) String usPassword,
                                         @RequestParam Integer usRole,
                                         HttpSession session) throws Exception {

        User user = new User();
        user.setUsAccount(usAccount);
        user.setUsPassword(usPassword);
        user.setUsRole(usRole);
        if (usRole == 3) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            user.setUsId(-1);
            map.put("data", user);
            map.put("code", 0);
            return map;
        } else {
            return userService.getUserInfo(user, session);
        }
    }

    /**
     * 用户注册
     *
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping("/register")
    @ResponseBody
    @ApiOperation(value = "姓名，账户，密码，身份证，用户角色，头像地址", notes = "注册")
    public HashMap<String, Object> register(@RequestBody User user) throws Exception {
        return userService.register(user);
    }

    @PostMapping("/examUser")
    @ResponseBody
    public HashMap<String, Object> examUser(@RequestParam Integer usId,
                                            @RequestParam Integer usExam,
                                            @RequestParam(required = false) String usComment) {
        return userService.examUser(usId, usExam, usComment);
    }

    @PostMapping("/retrievePassword")
    @ResponseBody
    @ApiOperation(value = "账户，手机号验证码，密码", notes = "忘记密码")
    public HashMap<String, Object> retrievePassword(@RequestParam(value = "usAccount") String usAccount,
                                                    @RequestParam(value = "phoneCode", required = false) String phoneCode,
                                                    @RequestParam(value = "usPassword", required = false) String usPassword,
                                                    @RequestParam(value = "type") Integer type) {
        System.out.println("调用接口:retrievePassword");
        System.out.println("usAccount:" + usAccount);
        System.out.println("phoneCode:" + phoneCode);
        System.out.println("usPassword:" + usPassword);
        System.out.println("type:" + type);
        return userService.updateUserInfo(usAccount, phoneCode, usPassword, type);
    }

    @DeleteMapping("/deleteUserByUsId")
    @ResponseBody
    public HashMap<String, Object> deleteUserByUsId(@RequestParam Integer usId) {
        return userService.deleteUserByUsId(usId);
    }

    @GetMapping("/getUserList")
    @ResponseBody
    @ApiOperation(value = "用户名，身份证，审核状态，账户，页数，行数", notes = "条件查询用户信息")
    public HashMap<String, Object> getUserList(@RequestParam(required = false) String usName,
                                               @RequestParam(required = false) String usIdentity,
                                               @RequestParam(required = false) String usExam,
                                               @RequestParam(required = false) String usAccount,
                                               @RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
                                               @RequestParam(value = "pageSize", defaultValue = "20", required = false) Integer pageSize) {
        return userService.getUserList(usName, usIdentity, usExam, usAccount, pageNo, pageSize);
    }

    @GetMapping("/getUserListNoWorkArea")
    @ResponseBody
    public HashMap<String, Object> getUserListNoWorkArea() {
        return userService.getUserListNoWorkArea();
    }

    @GetMapping("/getUserInfoByUsId")
    @ResponseBody
    @ApiOperation(value = "用户id", notes = "通过ID查找用户")
    public HashMap<String, Object> getUserInfoByUsId(@RequestParam Integer usId) {
        return userService.getUserInfoByUsId(usId);
    }

    @PostMapping("/batchExam")
    @ResponseBody
    public HashMap<String, Object> batchExam(@RequestBody UserDTO userDTO) {
        return userService.batchExam(userDTO.getUsId(), userDTO.getUsExam(), userDTO.getUsComment());
    }

    @PostMapping("/updateUserInfoById")
    @ResponseBody
    public HashMap<String, Object> updateUserInfoById(@RequestParam(value = "usId") Integer usId,
                                                      @RequestParam(value = "usName",required = false) String usName,
                                                      @RequestParam(value = "usIdentity",required = false) String usIdentity,
                                                      @RequestParam(value = "usPassword",required = false) String usPassword) {
        User user = new User();
        user.setUsId(usId);
        user.setUsName(usName);
        user.setUsIdentity(usIdentity);
        user.setUsPassword(usPassword);
        return userService.updateUserInfoById(user);
    }

    @GetMapping("/getUserListExam")
    @ResponseBody
    public HashMap<String, Object> getUserListExam() {
        return userService.getUserListExam();
    }

    /**
     * gbq
     * 用户权限管理
     */
    @GetMapping("/ControlRoleById")
    @ResponseBody
    public HashMap<String, Object> ControlRoleById(HttpSession session, @RequestParam(value = "usRole") Integer usRole) {
        return userService.ControlRoleById((Integer)session.getAttribute("usId"), usRole);
    }
}